using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameFramework.MathLibraries
{
    public class MathLibraries
    {
        public static Vector2 MidPoint(Vector2 start, Vector2 end)
        {
            return (start + end) / 2;
        }

        /// <summary>
        /// Return Trianle of ABC triangle
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <param name="C"></param>
        /// <returns></returns>
        public static float TriangleArea(Vector2 A, Vector2 B, Vector2 C)
        {
            return TriangleArea(A.X, A.Y, B.X, B.Y, C.X, C.Y);
        }
        public static float TriangleArea(float x1, float y1, float x2, float y2, float x3, float y3)
        {
            float a = x1 - x3;
            float b = y1 - y3;
            float c = x2 - x3;
            float d = y2 - y3;

            return 0.5f * Math.Abs((a * d) - (b * c));
        }

        public static bool CheckPointIntriangle(Vector2 P, Vector2 A, Vector2 B, Vector2 C)
        {
            float originalArea = TriangleArea(A, B, C);
            float trianglePAB = TriangleArea(P, A, B);
            float trianglePBC = TriangleArea(P, B, C);
            float trianglePCA = TriangleArea(P, C, A);

            return originalArea == (trianglePAB + trianglePBC + trianglePCA);
        }

        public static bool CheckPointInRectangle(Rectangle rect, Vector2 point)
        {
            if (point.X >= rect.X &&
                point.Y >= rect.Y &&
                point.X <= rect.X + rect.Width &&
                point.Y <= rect.Y + rect.Height)
            {
                return true;
            }
            return false;
        }
    }
}
